//============================================================================================
/**
 * @file	shop_poketch.c
 * @brief	ショップ下画面インターフェース画面
 * @author	AkitoMori GAME FREAK inc.
 */
//============================================================================================

// でんわテスト
//#define DEBUG_PHONE

#include "common.h"
#include "tcb.h"
#include "system\arc_util.h"
#include "system/clact_tool.h"
#include "system/lib_pack.h"
#include "system/snd_tool.h"
#include "system/fontoam.h"
#include "system/fontproc.h"
#include "system/touch_subwindow.h"
#include "system/pmfprint.h"
#include "system/wipe.h"
#include "gflib/touchpanel.h"
#include "system/window.h"
#include "field/poketch.h"
#include "field/syswork.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_fieldmenu.h"
#include "system\wordset.h"
#include "system\msgdata_util.h"
#include "system/bmp_menu.h"
#include "system/bmp_list.h"
#include "../field_clact.h"
#include "field/sysflag.h"
#include "field/fld_pmtel.h"
#include "field/fld_bmp.h"
#include "field/field_poketch.h"

#include "../report.h"

#include "poketch_arc.h"
#include "shop_gra.naix"
#include "msgdata/msg_common_scr.h"

#include "itemtool/item.h"
#include "msgdata/msg_bag.h"
#include "msgdata/msg_shop.h"
#include "msgdata/msg_itempocket.h"
#include "msgdata/msg_itemname.h"

#include "../fd_shop_sys.h"
#include "../fd_shop.h"

#define MENU_OAM_NUM		( 14 )
#define SHORTCUT_OAM_NUM	( 3 )
#define CURSOR_OAM_NUM		( 4 )
#define MENU_ALL_OAM_NUM	( MENU_OAM_NUM + SHORTCUT_OAM_NUM + CURSOR_OAM_NUM )

#define MENU_MAX			(  9 )

// CLACTで定義しているセルが大きすぎてサブ画面に影響がでてしまうので離してみる
#define SUBLCD_MENU_SUB_ACTOR_DISTANCE 	(256*FX32_ONE)

// CellActorに処理させるリソースマネージャの種類の数（＝マルチセル・マルチセルアニメは使用しない）
#define CLACT_RESOURCE_TYPE_NUM		(  4 )

// システムメニューのセルアクターが読み込むキャラ・パレット（セットで）の総数
#define CLACT_RESOURCE_CHARA_NUM	( 10 )

#define SUBLCD_OAM_NUM			( 13 )
#define CLACT_RESOURCE_TBL_NUM	(  2 )
#define REPORT_END_WAIT			( 30 )

#define HEAPID_SUBLCD_MENU		( HEAPID_POKETCH_APP )
#define ARC_SUBLCDMENU_GRA		( ARC_POKETCH_IMG )

#define	WORDSET_SHOPSUBLCD_BUFLEN	( 64 )		// ワードセットの文字数


// ウィンドウインデックス
enum {
	WIN_MENU_INFO,	// 上画面のどうぐ説明テキストウインドウ
	WIN_TALK,		// 下画面の会話ウインドウ

	WIN_PAGE,		// ページ番号「１／３」など
	WIN_CANCEL,		// 「やめる」
	WIN_GOLD,		// 所持金

	WIN_MAX,
};


enum {
	ADD_WIN_ITEM0=0,		// アイテム名描画６個
	ADD_WIN_ITEM1,
	ADD_WIN_ITEM2,
	ADD_WIN_ITEM3,
	ADD_WIN_ITEM4,
	ADD_WIN_ITEM5,

	ADD_WIN_SELECT_ITEM,// 選択されたアイテム名をセンターに描画する

	ADD_WIN_NUM_10,		// ３桁の数字10の列
	ADD_WIN_NUM_1,		// ３桁の数字1の列
	ADD_WIN_BUY,		// 「かう」

	ADD_WIN_INHAND,		// 所持しているどうぐの数
	ADD_WIN_PAY,		// 支払う金額

	ADD_WIN_MINITALK,	// 小さい会話ウインドウ

	ADD_SUB_WIN_MAX,

};


enum{
	SHOP_FADEIN_WAIT=0,
	SHOP_MAIN,

};

enum{
	TOUCH_MODE_LIST=0,
	TOUCH_MODE_NUM,
};

//------------------------------------------------------------------
/// 構造体定義
//------------------------------------------------------------------
typedef struct {
	int     	screen;
	GF_BGL_INI	*bgl;
	TCB_PTR 	tcb;
	int			seq;
	int			wait;
	SHOP_WORK   *shop_work;

	u16         *panel_trg;
	FIELDSYS_WORK    *fsys;

	GF_BGL_BMPWIN			MenuBmp;			// 選択中の項目を説明する文章を表示するBMPWIN領域
	int						MenuNameOn;			// MenuBmpを表示しているか。
	GF_BGL_BMPWIN	win[WIN_MAX];				// BMPウィンドウデータ（通常）
	GF_BGL_BMPWIN	sub_win[ADD_SUB_WIN_MAX];	// 場面場面で追加する

	WORDSET			*wordset;
	MSGDATA_MANAGER *msgman;
	MSGDATA_MANAGER *item_man;

	REPORT_INFO 			*riw;
	CONFIG 					*config;
	MYSTATUS				*my;
	PKTHLN_SV_DATA_PTR		pkthln_sv;

	//はい・いいえタッチパネル関連
	TOUCH_SW_SYS*			tpSWsys;

	GF_BGL_BMPWIN			TalkWinBmp;			// 選択中の項目を説明する文章を表示するBMPWIN領域
	int						msg_index;
	STRBUF					*talkstr;

	void					*TimeWaitIconPtr;	//

}SHOP_SUBLCD_WORK;

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
// サブシーケンス
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------





// パレット定義
#define	SYS_FONT_PAL	(  4 )		// システムフォント
#define	WINDOW_PAL		( 14 )		// ウィンドウ枠
#define	ICON_PAL		( 13 )		// アイコンパレット
#define	TALKWIN_PAL		( 12 )		// 会話ウィンドウ枠
#define	TALK_FONT_PAL	( 11 )		// 会話フォント

#define BAG_BMP_YESNO_CHAROFFSET	( 1+32*3 )	// 背景のキャラクタファイルである

// BMPデータ
// メニュー情報(下画面の上に表示される
// 1
#define	BMP_MENU_INFO_PX	(  2 )
#define	BMP_MENU_INFO_PY	(  1 )
#define	BMP_MENU_INFO_SX	( 27 )
#define	BMP_MENU_INFO_SY	(  2 )
#define	BMP_MENU_INFO_PAL	( TALK_FONT_PAL )
#define	BMP_MENU_INFO_CGX	( 1 )
#define	BMP_MENU_INFO_FRM	( GF_BGL_FRAME0_S )

// ショップのどうぐ表示のページ数／MAX（?? / ?? )
#define BMP_PAGE_PX		( 10 )
#define BMP_PAGE_PY		( 21 )
#define BMP_PAGE_W		(  7 )
#define BMP_PAGE_H		(  2 )
//#define	BMP_PAGE_PAL	( TALK_FONT_PAL )
#define	BMP_PAGE_PAL	( TALKWIN_PAL )
#define BMP_PAGE_CGX	( 1 )
#define	BMP_PAGE_FRM	( GF_BGL_FRAME0_S )


// 「やめる」
#define BMP_CANCEL_PX	( 25 )
#define BMP_CANCEL_PY	( 21 )
#define BMP_CANCEL_W	(  6 )
#define BMP_CANCEL_H	(  2 )
//#define	BMP_CANCEL_PAL	( TALK_FONT_PAL )
#define	BMP_CANCEL_PAL	( TALKWIN_PAL )
#define BMP_CANCEL_CGX	( BMP_PAGE_CGX + BMP_PAGE_W*BMP_PAGE_H )
#define	BMP_CANCEL_FRM	( GF_BGL_FRAME0_S )


// 会話ウィンドウ
#define	BMP_TALK_PX		( 2 )
#define	BMP_TALK_PY		( 1 )
#define	BMP_TALK_SX		( 27 )
#define	BMP_TALK_SY		( 4 )
#define	BMP_TALK_PAL	( TALK_FONT_PAL )
#define	BMP_TALK_CGX	( BMP_CANCEL_CGX + BMP_CANCEL_W * BMP_CANCEL_H )
#define	BMP_TALK_FRM	( GF_BGL_FRAME0_S )


// おこずかい
#define	BMP_GOLD_PX		( 1 )
#define	BMP_GOLD_PY		( 0 )
#define	BMP_GOLD_SX		( 9 )
#define	BMP_GOLD_SY		( 4 )
#define	BMP_GOLD_PAL	( TALK_FONT_PAL )
#define	BMP_GOLD_CGX	( BMP_TALK_CGX + BMP_TALK_SX * BMP_TALK_SY )
#define	BMP_GOLD_FRM	( GF_BGL_FRAME0_S )


// 追加BMPのキャラ開始位置
#define	ADD_SUB_CGX		( BMP_GOLD_CGX + BMP_GOLD_SX*BMP_GOLD_SY )
//#define	ADD_SUB_CGX		( BMP_MINITALK_CGX + BMP_MINITALK_W*BMP_MINITALK_H )

// アイテムリスト
#define	BMP_LIST_PX		( 14 )
#define	BMP_LIST_PY		( 0 )
#define	BMP_LIST_SX		( 17 )
#define	BMP_LIST_SY		( 18 )
#define	BMP_LIST_PAL	( TALKWIN_PAL )
#define	BMP_LIST_CGX	( ADD_SUB_CGX )
#define	BMP_LIST_FRM	( GF_BGL_FRAME0_S )

#define BMP_ITEM0_PX	(  4 )
#define BMP_ITEM1_PX	( 20 )
#define BMP_ITEM2_PX	(  4 )
#define BMP_ITEM3_PX	( 20 )
#define BMP_ITEM4_PX	(  4 )
#define BMP_ITEM5_PX	( 20 )

#define BMP_ITEM0_PY	(  5 )
#define BMP_ITEM1_PY	(  5 )
#define BMP_ITEM2_PY	( 10 )
#define BMP_ITEM3_PY	( 10 )
#define BMP_ITEM4_PY	( 15 )
#define BMP_ITEM5_PY	( 15 )

#define BMP_ITEM_W		( 11 )
#define BMP_ITEM_H		(  4 )



// 選択されたどうぐ名
#define	BMP_SELECTITEM_PAL	( TALKWIN_PAL )
#define BMP_SELECTITEM_PX	( 12 )
#define BMP_SELECTITEM_PY	(  7 )
#define BMP_SELECTITEM_W	( 11 )
#define BMP_SELECTITEM_H	(  4 )
#define	BMP_SELECTITEM_CGX	( ADD_SUB_CGX )
#define	BMP_SELECTITEM_FRM	( BMP_LIST_FRM )

// 数字表示BMP定義（１００の桁・１０の桁・１の桁を別々に描画）
#define BMP_NUM_10_PX	( 16 )
#define BMP_NUM_10_PY	( 14 )
#define BMP_NUM_1_PX	( 20 )
#define BMP_NUM_1_PY	( 14 )
#define BMP_NUM_W		(  2 )
#define BMP_NUM_H		(  3 )
#define BMP_NUM_PAL  	( TALK_FONT_PAL )
#define BMP_NUM_10_CGX	( BMP_SELECTITEM_CGX + BMP_SELECTITEM_W*BMP_SELECTITEM_H )
#define BMP_NUM_1_CGX	( BMP_NUM_10_CGX + BMP_NUM_W*BMP_NUM_H )
#define	BMP_NUM_FRM		( GF_BGL_FRAME0_S )

// 「かう」文字列領域
#define BMP_BUY_PX	( 18 )
#define BMP_BUY_PY	( 21 )
#define BMP_BUY_W	( 6 )
#define BMP_BUY_H	( 2 )
#define BMP_BUY_PAL ( TALK_FONT_PAL )
#define BMP_BUY_CGX	( BMP_NUM_1_CGX + (BMP_NUM_W*BMP_NUM_H) )
#define	BMP_BUY_FRM	( GF_BGL_FRAME0_S )

// 所持しているどうぐのかず
#define BMP_INHAND_PX	( 1 )
#define BMP_INHAND_PY	( 13 )
#define BMP_INHAND_W	( 8 )
#define BMP_INHAND_H	( 5 )
#define BMP_INHAND_PAL  ( TALK_FONT_PAL )
#define BMP_INHAND_CGX	( BMP_BUY_CGX + (BMP_BUY_W*BMP_BUY_H) )
#define	BMP_INHAND_FRM	( GF_BGL_FRAME0_S )

// 支払う金額
#define BMP_PAY_PX	( 23 )
#define BMP_PAY_PY	( 14 )
#define BMP_PAY_W	( 8 )
#define BMP_PAY_H	( 3 )
#define BMP_PAY_PAL ( TALK_FONT_PAL )
#define BMP_PAY_CGX	( BMP_INHAND_CGX + (BMP_INHAND_W*BMP_INHAND_H) )
#define	BMP_PAY_FRM	( GF_BGL_FRAME0_S )


// どうぐを売る画面用会話ウインドウ
#define BMP_MINITALK_PX	 ( 12 )
#define BMP_MINITALK_PY	 (  1 )
#define BMP_MINITALK_W	 ( 17 )
#define BMP_MINITALK_H	 (  4 )
#define	BMP_MINITALK_PAL ( TALK_FONT_PAL )
#define BMP_MINITALK_CGX ( BMP_PAY_CGX+BMP_PAY_W*BMP_PAY_H )
#define	BMP_MINITALK_FRM ( GF_BGL_FRAME0_S )

// 追加分も合わせたCGX最終地点
#define TALK_WIN_FRAME_CGX	( BMP_LIST_CGX+ (BMP_ITEM_W*BMP_ITEM_H)*6 )
#define BMP_ADD_WIN_CGX	 	( BMP_MINITALK_CGX+ BMP_MINITALK_W*BMP_MINITALK_H )



//------------------------------------------------------------------
/// プロトタイプ宣言
//------------------------------------------------------------------
static void _bg_set( SHOP_SUBLCD_WORK *rsw );
static void _bg_init( SHOP_SUBLCD_WORK *wk );
static void _bg_release( SHOP_SUBLCD_WORK *rsw );
static void _sublcd_task( TCB_PTR tcb, void *wk );



static void shop_sublcd_bmp_Init( SHOP_SUBLCD_WORK * wk );
static void ItemListBmp_WinInit( SHOP_SUBLCD_WORK *wk );


static void ShopItemNamePrint( SHOP_SUBLCD_WORK *wk  );
static void itemname_print( SHOP_SUBLCD_WORK *wk,  GF_BGL_BMPWIN *win, STRBUF *str, int index );
static void	ItemPricePrint( WORDSET *wset, MSGDATA_MANAGER *msg_man, GF_BGL_BMPWIN *win, int price, int type );
static void _msg_init( SHOP_SUBLCD_WORK *wk );
static void _msg_release( SHOP_SUBLCD_WORK *wk );
static void _write_bmpwin( SHOP_SUBLCD_WORK *wk, u32 mode );
static void _shop_gold_print( SHOP_SUBLCD_WORK * wk );

static void make_button_screen( GF_BGL_INI *bgl, int frm, int num );
static void _button_screen_set( SHOP_SUBLCD_WORK *wk, int num );
static u32 _touch_control( SHOP_SUBLCD_WORK *wk, int mode );
static BOOL _touch_select_check( SHOP_SUBLCD_WORK *wk, u32 ret );
static void _sublcd_page_refresh( SHOP_SUBLCD_WORK *wk, int refresh );
static void _page_set( SHOP_SUBLCD_WORK *wk, int mode );
static void _shop_page_print( SHOP_SUBLCD_WORK *wk );
static void SelectItemBmp_Del( SHOP_SUBLCD_WORK *wk );
static void SelectItemBmp_Init( SHOP_SUBLCD_WORK *wk );
static void _select_item_print( SHOP_SUBLCD_WORK *wk );
static void ItemListBmp_WinDel( SHOP_SUBLCD_WORK *wk );
static void _inhand_num_print( SHOP_SUBLCD_WORK *wk, int item );
static void _pay_num_print( SHOP_SUBLCD_WORK *wk, int sel_num );
static void _buy_str_print( SHOP_SUBLCD_WORK *wk );
static void ShopBuyItemNameWordSet( int type, WORDSET *wset, u16 item, u16 bufID );
static void _sale_talk_start( SHOP_SUBLCD_WORK *wk );
static void _price_talk_start( SHOP_SUBLCD_WORK *wk );
static void _touch_sw_yesno_start( SHOP_SUBLCD_WORK *wk );
static int  _touch_sw_yesno_wait( SHOP_SUBLCD_WORK *wk );
static void _item_buy_message_start( SHOP_SUBLCD_WORK *wk );
static void _cant_buy_talk_start( SHOP_SUBLCD_WORK *wk );
static void _cmp_false_talk_start( SHOP_SUBLCD_WORK *wk );
static void _cmp_false_talk_start2( SHOP_SUBLCD_WORK *wk );
static void _puremium_message_talk_start( SHOP_SUBLCD_WORK* wk );
static  int _price_print_check( SHOP_WORK *sw, int pos, int item );
static STRBUF *_get_cant_buy_string( SHOP_WORK *wk, MSGDATA_MANAGER *msgman );
static STRBUF *_get_cmp_false_string( SHOP_WORK *wk, MSGDATA_MANAGER *msgman );

static BOOL TalkMsgCallBack( MSG_PRINT_HEADER * mph, u16 value );


#define	BAGCOL_N_BLACK	( GF_PRINTCOLOR_MAKE( 1, 2, 0 ) )		// フォントカラー：黒

static const TP_HIT_TBL mes_touch_tbl[]={
	{0,191, 0,255 },
};

//------------------------------------------------------------------
/**
 * ポケッチ取得以前の画面構築
 *
 * @param   none
 *
 */
//------------------------------------------------------------------
TCB_PTR ShopSublcdInit( GF_BGL_INI* bgl, u16 *sub_panel_trg, FIELDSYS_WORK *fsys, void *work )
{
	TCB_PTR       tcb;


	// 異様にデカいがポケッチの合計と同じにしておく
	sys_CreateHeap( HEAPID_BASE_APP, HEAPID_SUBLCD_MENU,  0x18000 );

	{
		SHOP_SUBLCD_WORK *rsw;
		int i;

		tcb             = PMDS_taskAdd( _sublcd_task, sizeof(SHOP_SUBLCD_WORK), 10, HEAPID_SUBLCD_MENU);
		rsw             = TCB_GetWork(tcb);
		rsw->tcb        = tcb;
		rsw->screen     = 0;
		rsw->bgl        = bgl;
		rsw->panel_trg  = sub_panel_trg;
		rsw->fsys       = fsys;
		rsw->MenuNameOn = 0;
		rsw->config    	= SaveData_GetConfig(rsw->fsys->savedata);
		rsw->my			= SaveData_GetMyStatus( rsw->fsys->savedata );
		rsw->pkthln_sv  = PkthlnSv_GetSaveData(rsw->fsys->savedata);
		rsw->seq        = SHOP_FADEIN_WAIT;
		rsw->shop_work  = (SHOP_WORK*)work;

		// BG初期化
		_bg_init( rsw );
		// BG転送
		_bg_set( rsw );

		_msg_init( rsw );

//		_obj_init( rsw );

		OS_Printf("menu_on_flag = %d\n", rsw->fsys->sublcd_menu_on);


		_write_bmpwin( rsw, 0 );
		_shop_gold_print( rsw );
		_shop_page_print( rsw );


		ShopItemNamePrint( rsw );

		ShopBuyActSet( rsw->shop_work );
	}

	GXS_SetVisibleWnd( GX_WNDMASK_NONE );
//	GXS_SetVisiblePlane( GX_PLANEMASK_BG0|GX_PLANEMASK_BG1|GX_PLANEMASK_BG2 );
	GF_Disp_GXS_VisibleControl(GX_PLANEMASK_BG0, VISIBLE_ON);	//OBJ面ＯＮ
	GF_Disp_GXS_VisibleControl(GX_PLANEMASK_BG1, VISIBLE_ON);	//OBJ面ＯＮ
	GF_Disp_GXS_VisibleControl(GX_PLANEMASK_BG2, VISIBLE_ON);	//OBJ面ＯＮ
	GF_Disp_GXS_VisibleControl(GX_PLANEMASK_OBJ, VISIBLE_ON);	//OBJ面ＯＮ



	// 会話ウインドウタッチ有効
	MsgPrintTouchPanelFlagSet( MSG_TP_ON );
	MsgPrintTouchPanelRectSet( mes_touch_tbl );

	return tcb;
}

#define TALKMSG_BUFLEN	( 40*2+1 )

//------------------------------------------------------------------
/**
 * @brief   メッセージデータ周り初期化
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _msg_init( SHOP_SUBLCD_WORK *wk )
{
	wk->wordset  = WORDSET_CreateEx( WORDSET_DEFAULT_SETNUM, WORDSET_SHOPSUBLCD_BUFLEN, HEAPID_SUBLCD_MENU );
	wk->msgman   = MSGMAN_Create( MSGMAN_TYPE_NORMAL, ARC_MSG, NARC_msg_shop_dat, HEAPID_SUBLCD_MENU );
	wk->item_man = MSGMAN_Create( MSGMAN_TYPE_NORMAL, ARC_MSG, NARC_msg_itemname_dat, HEAPID_SUBLCD_MENU );

	wk->talkstr	 = STRBUF_Create( TALKMSG_BUFLEN, HEAPID_SUBLCD_MENU );
}

//------------------------------------------------------------------
/**
 * @brief   メッセージマネージャー解放
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _msg_release( SHOP_SUBLCD_WORK *wk )
{
	STRBUF_Delete( wk->talkstr );

	MSGMAN_Delete( wk->item_man );
	MSGMAN_Delete( wk->msgman );
	WORDSET_Delete( wk->wordset );
}

#define	BAGCOL_N_WHITE	( GF_PRINTCOLOR_MAKE( 15, 14, 0 ) )		// フォントカラー：白

//------------------------------------------------------------------
/**
 * @brief   初期BMPWIN描画
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _write_bmpwin( SHOP_SUBLCD_WORK *wk, u32 mode )
{
	STRBUF *str  = MSGMAN_AllocString( wk->msgman, mes_shop_02_06 );

	GF_BGL_BmpWinOff( &wk->win[WIN_CANCEL] );

	GF_BGL_BmpWinDataFill( &wk->win[WIN_CANCEL], 0 );

	if( mode == 0 ){
		GF_STR_PrintColor(
			&wk->win[WIN_CANCEL], FONT_SYSTEM, str,
			(GF_BGL_BmpWinGet_SizeX(&wk->win[WIN_CANCEL])*8-FontProc_GetPrintStrWidth(FONT_SYSTEM,str,0))/2,
			0,
			MSG_NO_PUT, BAGCOL_N_WHITE, NULL );
	}else{
		GF_STR_PrintColor(
			&wk->win[WIN_CANCEL], FONT_SYSTEM, str,
			13, 0, MSG_NO_PUT, BAGCOL_N_WHITE, NULL );
	}

	GF_BGL_BmpWinOnVReq( &wk->win[WIN_CANCEL] );

	STRBUF_Delete( str );
}

//==============================================================================
/**
 * @brief   終了リクエスト
 *
 * @param   bgl
 * @param   tcb
 *
 * @retval  none
 */
//==============================================================================
void ShopSublcdQuitReq( GF_BGL_INI* bgl,  TCB_PTR tcb )
{
	int i;

	SHOP_SUBLCD_WORK *rsw = TCB_GetWork(tcb);

	OS_Printf("tcb=%08x\n",tcb);


	// 会話ウインドウタッチ有効
	MsgPrintTouchPanelFlagSet( MSG_TP_OFF );


	// BG周り解放
	_bg_release( rsw );

	// メッセージマネージャー解放
	_msg_release( rsw );

	PMDS_taskDel( tcb );

	GF_BGL_BGControlExit( bgl, GF_BGL_FRAME2_S );
	GF_BGL_BGControlExit( bgl, GF_BGL_FRAME1_S );
	GF_BGL_BGControlExit( bgl, GF_BGL_FRAME0_S );
	sys_DeleteHeap( HEAPID_SUBLCD_MENU );
}

//==============================================================================
/**
 * @brief   終了待ち（即終了）
 *
 * @param   bgl
 *
 * @retval  BOOL
 */
//==============================================================================
BOOL ShopSublcdQuitWait( GF_BGL_INI* bgl )
{
	return TRUE;
}


//------------------------------------------------------------------
/**
 * @brief   タッチした内容で反応するかどうかチェックする
 *
 * @param   wk
 * @param   ret
 *
 * @retval  BOOL
 */
//------------------------------------------------------------------
static BOOL _touch_select_check( SHOP_SUBLCD_WORK *wk, u32 ret )
{
	switch(ret){
	case CURSORPOS_ITEM0: case CURSORPOS_ITEM1: case CURSORPOS_ITEM2:
	case CURSORPOS_ITEM3: case CURSORPOS_ITEM4: case CURSORPOS_ITEM5:
		// どうぐの販売数のMAXを超えていない
		if((ret+wk->shop_work->scr) < wk->shop_work->max){
			return TRUE;
		}
		break;
	case CURSORPOS_SUBPAGE:	case CURSORPOS_ADDPAGE: case CURSORPOS_END:
		return TRUE;
	}
	return FALSE;
}


#define TALK_BMPWIN_CGX			(32*4)
#define TALK_WINDOW_CGX			(TALK_BMPWIN_CGX+(FLD_MSG_WIN_SX*FLD_MSG_WIN_SY))
#define TALK_WINDOW_PAL			(  5 )
#define TALK_STRING_PAL		 	(  4 )

//------------------------------------------------------------------
/**
 * @brief   下画面ショップインターフェース
 *
 * @param   tcb
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _sublcd_task( TCB_PTR tcb, void *wk )
{
	SHOP_SUBLCD_WORK *rsw = (SHOP_SUBLCD_WORK *)wk;
	int ret;
	// fd_shop.cから
	// FieldSubLcdControler_ChangeAppによって切り替えられるのを待つ
	switch(rsw->seq){
	case SHOP_FADEIN_WAIT:
		if(WIPE_SYS_EndCheck()==TRUE){
			rsw->seq = SHOP_MAIN;

		}
		break;
	case SHOP_MAIN:
		switch(rsw->shop_work->seq){
		case SHOP_SEQ_BUY_SELECT:
			if((ret = _touch_control(rsw, TOUCH_MODE_LIST))!=RECT_HIT_NONE){
				if(_touch_select_check(rsw, ret)){
					rsw->shop_work->touch_arret = ret;
				}
			}
			break;
		case SHOP_SEQ_BUYNUM_SEL:
			if((ret = _touch_control(rsw, TOUCH_MODE_NUM))!=RECT_HIT_NONE){
				rsw->shop_work->touch_arret = ret;
			}
			break;
		case SHOP_SEQ_BUY_YESNO:
			_touch_sw_yesno_wait( wk );
				break;
		}

		// 再描画コマンドが来たら画面構築を行う
		if(rsw->shop_work->sublcd_refresh){
			_sublcd_page_refresh( wk, rsw->shop_work->sublcd_refresh );
			rsw->shop_work->sublcd_refresh = 0;
		}

		break;
	}

}

//------------------------------------------------------------------
/**
 * @brief   下画面再描画処理
 *
 * @param   wk
 * @param   refresh		1:ページ切り替え 2:どうぐ選択 3:リスト画面に復帰
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _sublcd_page_refresh( SHOP_SUBLCD_WORK *wk, int refresh )
{
	switch(refresh){
	// ページ切り替え(次ページ表示の際）
	case SUBLCD_REFRESH_LIST:
		ShopItemNamePrint( wk );
		_shop_page_print( wk );
		_write_bmpwin( wk, 0 );
		break;

	// どうぐを選択した後の画面へ
	case SUBLCD_REFRESH_SELECT:
		_page_set( wk, SHOP_MODE_SELECT );
		ItemListBmp_WinDel( wk );
		SelectItemBmp_Init( wk );
		_select_item_print( wk );
		_inhand_num_print( wk, wk->shop_work->sel_item );
		_pay_num_print( wk, wk->shop_work->sel_num );
		_buy_str_print( wk );
		_write_bmpwin( wk, 1 );
		_sale_talk_start( wk );
		GF_BGL_BmpWinOffVReq( &wk->win[WIN_PAGE] );
		break;

	// どうぐを選択した後の画面へ（個数選択なし）
	case SUBLCD_REFRESH_SELECT_EX:
		_page_set( wk, SHOP_MODE_SELECT_EX );
		ItemListBmp_WinDel( wk );
		SelectItemBmp_Init( wk );
		_select_item_print( wk );
		_inhand_num_print( wk, wk->shop_work->sel_item );
		_price_talk_start( wk );
		GF_BGL_BmpWinOffVReq( &wk->win[WIN_PAGE] );
		GF_BGL_BmpWinOffVReq( &wk->win[WIN_CANCEL] );
		break;

	// リスト画面に復帰
	case SUBLCD_REFRESH_RELIST:
		_page_set( wk, SHOP_MODE_LIST );
		SelectItemBmp_Del( wk );
		_shop_page_print( wk );
		_write_bmpwin( wk, 0 );
		ItemListBmp_WinInit( wk );
		BmpTalkWinClear( &wk->win[WIN_TALK], WINDOW_TRANS_OFF );
		ShopItemNamePrint( wk  );
		_shop_gold_print( wk );
		GF_BGL_BmpWinOnVReq( &wk->win[WIN_CANCEL] );
		break;

	// どうぐ選択画面までいかなった時にリストの再描画だけ行う
	case SUBLCD_REFRESH_LIST_ONLY:
		_page_set( wk, SHOP_MODE_LIST );
		_shop_page_print( wk );
		BmpTalkWinClear( &wk->win[WIN_TALK], WINDOW_TRANS_ON );
		ShopItemNamePrint( wk  );
		_shop_gold_print( wk );
		break;

	// 購入数指定時の数値表示更新
	case SUBLCD_REFRESH_SELECT_NUM:
		_pay_num_print( wk, wk->shop_work->sel_num );
		break;

	// 「●●を■■個買いますか？」表示
	case SUBLCD_REFRESH_PRICE_TALK:
		GF_BGL_BmpWinOffVReq( &wk->win[WIN_CANCEL] );
		GF_BGL_BmpWinOffVReq( &wk->sub_win[ADD_WIN_BUY] );
		_price_talk_start( wk );
		break;

	// はい・いいえ
	case SUBLCD_REFRESH_BUY_YESNO:
		_touch_sw_yesno_start( wk );
		break;

	// 買いました。
	case SUBLCD_REFRESH_BUY_MESSAGE:
		_item_buy_message_start( wk );
		break;

	// お金が足りませんよ
	case SUBLCD_REFRESH_CANT_BUY:
		_cant_buy_talk_start( wk );
		MORI_PRINT( "talk win cgx = %d\n", TALK_WIN_FRAME_CGX );
		break;


	// ＸＸが いっぱい
	case SUBLCD_REFRESH_CMP_FALSE:
		GF_BGL_BmpWinOffVReq( &wk->win[WIN_CANCEL] );
		GF_BGL_BmpWinOffVReq( &wk->sub_win[ADD_WIN_BUY] );
		_cmp_false_talk_start( wk );
		break;

	// ＸＸが いっぱい（個数選択なし）
	case SUBLCD_REFRESH_CMP_FALSE_EX:
		_cmp_false_talk_start2( wk );
		break;

	case SUBLCD_REFRESH_PREMIUM_MESSAGE:
		_puremium_message_talk_start( wk );
		break;
	}

}


//------------------------------------------------------------------
/**
 * @brief   BGスクリーン転送を行う
 *
 * @param   wk
 * @param   mode	SHOP_MODE_LIST, SHOP_MODE_SELECT
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _page_set( SHOP_SUBLCD_WORK *wk, int mode )
{
	NNSG2dScreenData *scrnData;
	void 			 *scrnBuf;

	switch(mode){
	// どうぐ選択画面を表示
	case SHOP_MODE_LIST:
		scrnBuf = ArcUtil_ScrnDataGet(ARC_SHOP_GRA, NARC_shop_gra_itemBG01_d_NSCR,0, &scrnData, HEAPID_POKETCH_APP);
		GF_BGL_ScreenBufSet( wk->bgl, GF_BGL_FRAME2_S, scrnData->rawData, scrnData->szByte );
		GF_BGL_LoadScreenV_Req( wk->bgl, GF_BGL_FRAME2_S );
		sys_FreeMemoryEz(scrnBuf);
		GF_Disp_GXS_VisibleControl( GX_PLANEMASK_BG1, VISIBLE_ON);	//OBJ面ＯＮ
		break;

	// 選択したどうぐを中央に表示する
	case SHOP_MODE_SELECT:
		GF_BGL_ScrClearCodeVReq( wk->bgl, GF_BGL_FRAME1_S, 0 );
//		ArcUtil_ScrnSet(ARC_SHOP_GRA, NARC_shop_gra_itemBG03_d_NSCR, wk->bgl, GF_BGL_FRAME2_S, 0, 0, 0, HEAPID_SUBLCD_MENU);
		scrnBuf = ArcUtil_ScrnDataGet(ARC_SHOP_GRA, NARC_shop_gra_itemBG03_d_NSCR,0, &scrnData, HEAPID_SUBLCD_MENU);
		GF_BGL_ScreenBufSet( wk->bgl, GF_BGL_FRAME2_S, scrnData->rawData, scrnData->szByte );
		GF_BGL_LoadScreenV_Req( wk->bgl, GF_BGL_FRAME2_S );
		sys_FreeMemoryEz(scrnBuf);
		break;

	// 選択したどうぐを中央に表示する
	case SHOP_MODE_SELECT_EX:
		GF_BGL_ScrClearCodeVReq( wk->bgl, GF_BGL_FRAME1_S, 0 );
//		ArcUtil_ScrnSet(ARC_SHOP_GRA, NARC_shop_gra_itemBG04_d_NSCR, wk->bgl, GF_BGL_FRAME2_S, 0, 0, 0, HEAPID_SUBLCD_MENU);
		scrnBuf = ArcUtil_ScrnDataGet(ARC_SHOP_GRA, NARC_shop_gra_itemBG04_d_NSCR,0, &scrnData, HEAPID_SUBLCD_MENU);
		GF_BGL_ScreenBufSet( wk->bgl, GF_BGL_FRAME2_S, scrnData->rawData, scrnData->szByte );
		GF_BGL_LoadScreenV_Req( wk->bgl, GF_BGL_FRAME2_S );
		sys_FreeMemoryEz(scrnBuf);
		break;
	}
}

// どうぐの初期状態用のタッチ用データ
static const RECT_HIT_TBL TouchButtonNormalTbl[]={
	{  32,  73,   0, 127,},		// どうぐ６つ
	{  32,  73, 128, 255,},
	{  74, 117,   0, 127,},
	{  74, 117, 128, 255,},
	{ 118, 153,   0, 127,},
	{ 118, 153, 128, 255,},

	{ 168, 191,   0,  39,},		// 下ページ
	{ 168, 191,  40,  79,},		// 上ページ
	{ 168, 191, 192, 255,},		// やめる

	{TP_HIT_END,0,0,0},		// 終了データ

};


// 数値入力時
static const RECT_HIT_TBL TouchButtonNumInputTbl[]={

	{  88, 111, 120, 151, },	// 10の桁アップ
	{  88, 111, 152, 183, },	// 1の桁アップ
	{ 136, 159, 120, 151, },	// 10の桁ダウン
	{ 136, 159, 152, 183, },	// 1の桁ダウン


	{ 168, 191, 128, 183,},		// かう
	{ 168, 191, 192, 255,},		// やめる

	{TP_HIT_END,0,0,0},			// 終了データ
};


//------------------------------------------------------------------
/**
 * @brief   タッチ処理
 *
 * @param   wk
 * @param   mode
 *
 * @retval  u32
 */
//------------------------------------------------------------------
static u32 _touch_control( SHOP_SUBLCD_WORK *wk, int mode )
{
	static const RECT_HIT_TBL *TouchTable[]={
		TouchButtonNormalTbl,       // 通常時
		TouchButtonNumInputTbl,		// 選択して数値入力と決定する時
	};
	int ret;

	GF_ASSERT( mode==0 || mode==1 );
	ret = GF_TP_RectHitTrg( TouchTable[mode] );

#ifdef PM_DEBUG
	if(ret!=RECT_HIT_NONE){
		MORI_PRINT("touch  = %d\n", ret);
	}
#endif

	return ret;
}


// ショップ画面セルアクター定義
enum{
	ITEM_ICON_0,
	ITEM_ICON_1,
	ITEM_ICON_2,
	ITEM_ICON_3,
	ITEM_ICON_4,
	ITEM_ICON_5,

	SHOP_SELECT_BUY,
	SHOP_SELECT_CANCEL,

	NUM_INPUT_10_UP,
	NUM_INPUT_1_UP,
	NUM_INPUT_10_DOWN,
	NUM_INPUT_1_DOWN,

	SHOP_OAM_MAX,
};




//------------------------------------------------------------------
/**
 * @brief   下画面BG初期化
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _bg_init( SHOP_SUBLCD_WORK *wk )
{
	static const GF_BGL_BGCNT_HEADER header[] = {
		{
			0, 0, 0x800, 0,	// scrX, scrY, scrbufSize, scrbufofs,
			GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0x6800, GX_BG_CHARBASE_0x00000,
			GX_BG_EXTPLTT_01, 0, 0, 0, FALSE	// pal, pri, areaover, dmy, mosaic
		},
		{
			0, 0, 0x800, 0,	// scrX, scrY, scrbufSize, scrbufofs,
			GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0x7000, GX_BG_CHARBASE_0x04000,
			GX_BG_EXTPLTT_01, 1, 0, 0, FALSE	// pal, pri, areaover, dmy, mosaic

		},
		{
			0, 0, 0x800, 0,	// scrX, scrY, scrbufSize, scrbufofs,
			GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0x7800, GX_BG_CHARBASE_0x04000,
			GX_BG_EXTPLTT_01, 2, 0, 0, FALSE	// pal, pri, areaover, dmy, mosaic

		},
	};

	// BG 設定
	GXS_SetGraphicsMode( GX_BGMODE_0 );
	GX_SetBankForSubBG( GX_VRAM_SUB_BG_32_H );
	GX_SetBankForSubOBJ( GX_VRAM_SUB_OBJ_16_I );
	GXS_SetOBJVRamModeChar( GX_OBJVRAMMODE_CHAR_1D_32K );


	GF_BGL_BGControlSet( wk->bgl, GF_BGL_FRAME0_S, &header[0], GF_BGL_MODE_TEXT );
	GF_BGL_BGControlSet( wk->bgl, GF_BGL_FRAME1_S, &header[1], GF_BGL_MODE_TEXT );
	GF_BGL_BGControlSet( wk->bgl, GF_BGL_FRAME2_S, &header[2], GF_BGL_MODE_TEXT );
	GF_BGL_ClearCharSet( GF_BGL_FRAME2_S, 32, 0, HEAPID_FIELD );
//	GF_BGL_ScrClear( wk->bgl, GF_BGL_FRAME0_S );
//	GF_BGL_ScrClear( wk->bgl, GF_BGL_FRAME1_S );
	GF_BGL_ScrClear( wk->bgl, GF_BGL_FRAME2_S );

}

//==============================================================================
/**
 * @brief   指定のパレット・キャラ・スクリーンを下画面用に転送
 *
 * @param   bgl
 *
 * @retval  none
 */
//==============================================================================
static void _bg_set( SHOP_SUBLCD_WORK *rsw )
{

	G2S_BlendNone();

	// パレット
	ArcUtil_PalSet(     ARC_SHOP_GRA, NARC_shop_gra_itemBG01_d_NCLR, PALTYPE_SUB_BG, 0, 0, HEAPID_POKETCH_APP );

	// キャラ
	ArcUtil_BgCharSet(	ARC_SHOP_GRA, NARC_shop_gra_itemBG_d_NCGR, rsw->bgl,
						GF_BGL_FRAME1_S, 0, 0, 0, HEAPID_POKETCH_APP );

	_page_set( rsw, SHOP_MODE_LIST );

	// 会話ウインドウグラフィックセット
//	TalkWinGraphicSet(
//		rsw->bgl, GF_BGL_FRAME0_S, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL,
//		CONFIG_GetWindowType(rsw->config), HEAPID_FIELD );

	// 会話フォントパレット
	TalkFontPaletteLoad( PALTYPE_SUB_BG, 11 * 32, HEAPID_POKETCH_APP );
	SystemFontPaletteLoad( PALTYPE_SUB_BG, 12*32, HEAPID_POKETCH_APP );


	// メニュー説明文字列用BMPWIN確保
//	GF_BGL_BmpWinAdd( rsw->bgl, &rsw->TalkWinBmp, GF_BGL_FRAME0_S,
//			FLD_MSG_WIN_PX,  FLD_MSG_WIN_PY,
//			FLD_MSG_WIN_SX, FLD_MSG_WIN_SY,
//			TALK_STRING_PAL,  TALK_BMPWIN_CGX );
//	GF_BGL_BmpWinDataFill( &rsw->TalkWinBmp, 0xff );

	shop_sublcd_bmp_Init( rsw );
	ItemListBmp_WinInit( rsw );

	OS_Printf("remain ram = %d\n", sys_GetHeapFreeSize( HEAPID_POKETCH_APP ));

}


//------------------------------------------------------------------
/**
 * @brief   ＢＧ周り解放
 *
 * @param   rsw
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _bg_release( SHOP_SUBLCD_WORK *rsw )
{
	// どうぐの名前表示用BMP解放
	ItemListBmp_WinDel( rsw );

	// BMPWIN解放
	GF_BGL_BmpWinDel( &rsw->win[WIN_MENU_INFO] );
	GF_BGL_BmpWinDel( &rsw->win[WIN_TALK] );
	GF_BGL_BmpWinDel( &rsw->win[WIN_GOLD] );
	GF_BGL_BmpWinDel( &rsw->win[WIN_PAGE] );
	GF_BGL_BmpWinDel( &rsw->win[WIN_CANCEL] );


	// 会話ウインドウ
//	GF_BGL_BmpWinDel( &rsw->TalkWinBmp );
}










static const int BmpItemTable[][3]={
	{ BMP_ITEM0_PX, BMP_ITEM0_PY, BMP_LIST_CGX + (BMP_ITEM_W*BMP_ITEM_H)*0 },
	{ BMP_ITEM1_PX, BMP_ITEM1_PY, BMP_LIST_CGX + (BMP_ITEM_W*BMP_ITEM_H)*1 },
	{ BMP_ITEM2_PX, BMP_ITEM2_PY, BMP_LIST_CGX + (BMP_ITEM_W*BMP_ITEM_H)*2 },
	{ BMP_ITEM3_PX, BMP_ITEM3_PY, BMP_LIST_CGX + (BMP_ITEM_W*BMP_ITEM_H)*3 },
	{ BMP_ITEM4_PX, BMP_ITEM4_PY, BMP_LIST_CGX + (BMP_ITEM_W*BMP_ITEM_H)*4 },
	{ BMP_ITEM5_PX, BMP_ITEM5_PY, BMP_LIST_CGX + (BMP_ITEM_W*BMP_ITEM_H)*5 },
};




//--------------------------------------------------------------------------------------------
/**
 * BMPウィンドウ設定
 *
 * @param	wk		バッグ画面ワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void shop_sublcd_bmp_Init( SHOP_SUBLCD_WORK * wk )
{
	int i;

	MORI_PRINT(" wincgx = %d, itemlist =%d, BMP_ADD_WIN_CGX = %d ",
					ADD_SUB_CGX, BMP_LIST_CGX+BMP_LIST_SX*BMP_LIST_SY, BMP_ADD_WIN_CGX  );

	// メニュー情報
	GF_BGL_BmpWinAdd(
		wk->bgl, &wk->win[WIN_MENU_INFO],
		BMP_MENU_INFO_FRM, BMP_MENU_INFO_PX, BMP_MENU_INFO_PY,
		BMP_MENU_INFO_SX, BMP_MENU_INFO_SY, BMP_MENU_INFO_PAL, BMP_MENU_INFO_CGX );


	// 会話ウインドウ
	GF_BGL_BmpWinAdd(
		wk->bgl, &wk->win[WIN_TALK],
		BMP_TALK_FRM, BMP_TALK_PX, BMP_TALK_PY,
		BMP_TALK_SX, BMP_TALK_SY, BMP_TALK_PAL, BMP_TALK_CGX
	);

	// おこづかい
	GF_BGL_BmpWinAdd(
		wk->bgl, &wk->win[WIN_GOLD],
		BMP_TALK_FRM, BMP_GOLD_PX, BMP_GOLD_PY,
		BMP_GOLD_SX, BMP_GOLD_SY, BMP_GOLD_PAL, BMP_GOLD_CGX );

	// 現在のページ数
	GF_BGL_BmpWinAdd(
		wk->bgl, &wk->win[WIN_PAGE],
		BMP_PAGE_FRM, BMP_PAGE_PX, BMP_PAGE_PY,
		BMP_PAGE_W, BMP_PAGE_H, BMP_PAGE_PAL, BMP_PAGE_CGX );
	GF_BGL_BmpWinDataFill( &wk->win[WIN_PAGE], 0 );

	// やめる
	GF_BGL_BmpWinAdd(
		wk->bgl, &wk->win[WIN_CANCEL],
		BMP_CANCEL_FRM, BMP_CANCEL_PX, BMP_CANCEL_PY,
		BMP_CANCEL_W, BMP_CANCEL_H, BMP_CANCEL_PAL, BMP_CANCEL_CGX );
	GF_BGL_BmpWinDataFill( &wk->win[WIN_CANCEL], 0 );


	// 追加BMP用ポインタのNULLクリア
	for(i=0;i<ADD_SUB_WIN_MAX;i++){
		wk->sub_win[i].ini = NULL;
	}



	OS_Printf("MENU_WIN_CGX_NUM=%d\n",MENU_WIN_CGX_NUM);
	OS_Printf("TALK_WIN_CGX_NUM=%d\n",TALK_WIN_CGX_NUM);

}


//------------------------------------------------------------------
/**
 * @brief   どうぐリストBMP領域の確保
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ItemListBmp_WinInit( SHOP_SUBLCD_WORK *wk )
{
	int i;
	// どうぐの名前表示用BMP
	for(i=0;i<SHOP_PAGE_ITEM_NUM;i++){
		GF_BGL_BmpWinAdd(
			wk->bgl, &wk->sub_win[ADD_WIN_ITEM0+i], BMP_LIST_FRM,
			BmpItemTable[i][0], BmpItemTable[i][1], BMP_ITEM_W, BMP_ITEM_H, BMP_LIST_PAL, BmpItemTable[i][2] );
	}
}


//------------------------------------------------------------------
/**
 * @brief   どうぐリストBMP領域の解放
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ItemListBmp_WinDel( SHOP_SUBLCD_WORK *wk )
{
	int i;
	// どうぐの名前表示用BMP解放
	for(i=0;i<SHOP_PAGE_ITEM_NUM;i++){
//		GF_BGL_BmpWinOffVReq( &wk->sub_win[ADD_WIN_ITEM0+i] );
		GF_BGL_BmpWinOff( &wk->sub_win[ADD_WIN_ITEM0+i] );
		GF_BGL_BmpWinDel( &wk->sub_win[ADD_WIN_ITEM0+i] );
	}
}


//==============================================================================
/**
 * @brief   どうぐ名描画
 *
 * @param   wk
 * @param   scr		スクロールポイント（金銀では６の倍数になる）
 * @param   pos		カーソル位置(ー１だと全て名前のみ、この値に０〜５が入っている場合はどうぐ移動モード）
 * @param   mode	ITEMPRINT_NORMAL:どうぐ通常表示  ITEMPRINT_INSERT:どうぐ移動モード
 *
 * @retval  none
 */
//==============================================================================
static void ShopItemNamePrint( SHOP_SUBLCD_WORK *wk  )
{
	int i,count,num;
	for(i=0;i<SHOP_PAGE_ITEM_NUM;i++){
		GF_BGL_BmpWinDataFill( &wk->sub_win[ADD_WIN_ITEM0+i], 0 );
	}

	num = (wk->shop_work->max - wk->shop_work->scr);
	num = MATH_CLAMP(num, 0, SHOP_PAGE_ITEM_NUM);		// nitroSDK関数 numをAからBの範囲の値に制限する


	MORI_PRINT("max = %d, scr=%d\n", wk->shop_work->max, wk->shop_work->scr);
	for(i=0;i<num;i++){
		int pos  = i+wk->shop_work->scr;
		int item = wk->shop_work->item[pos];
		// 名前描画
		STRBUF *str = MSGMAN_AllocString( wk->item_man, item );
		itemname_print( wk, &wk->sub_win[ADD_WIN_ITEM0+i], str, i );
		STRBUF_Delete(str);

		if(_price_print_check(wk->shop_work, pos, item)){
			// 値段描画
			ItemPricePrint( wk->wordset, wk->msgman, &wk->sub_win[ADD_WIN_ITEM0+i],
							ShopBuyItemPriceGet( wk->shop_work, item ),
							wk->shop_work->shop_type);
		}

	}
	for(i=0;i<SHOP_PAGE_ITEM_NUM;i++){
		GF_BGL_BmpWinOnVReq( &wk->sub_win[ADD_WIN_ITEM0+i] );
	}
	_button_screen_set( wk, num );

}


//------------------------------------------------------------------
/**
 * @brief   どうぐ名と所持数を描画する
 *
 * @param   wset		ワードセット
 * @param   msg_man		メッセージマネージャー
 * @param   win		    BMPWIN
 * @param   str		    文字列バッファ
 * @param   pocket		ポケット構造体ポンイタ
 * @param   index		ポケット内どうぐの位置番号
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void itemname_print( SHOP_SUBLCD_WORK *wk,  GF_BGL_BMPWIN *win, STRBUF *str, int index )
{

	// どうぐ名描画
	// 通常のどうぐ
	GF_STR_PrintColor(
		win, FONT_SYSTEM, str,	0, 0, MSG_NO_PUT, BAGCOL_N_BLACK, NULL );



}


#if 0
//--------------------------------------------------------------------------------------------
/**
 * アイテムの値段取得
 *
 * @param	wk		ショップワーク
 * @param	item	アイテムID
 *
 * @return	値段
 */
//--------------------------------------------------------------------------------------------
static u32 ShopBuyItemPriceGet( SHOP_WORK * wk, u16 item )
{
	if( wk->shop_type == SHOP_TYPE_ITEM ){
		return ItemParamGet( item, ITEM_PRM_PRICE, HEAPID_WORLD );
	}else if( wk->shop_type == SHOP_TYPE_GOODS ){
		return 100;
//		return GOODS_GetGoodsMoney( (const int)item );
	}else if( wk->shop_type == SHOP_TYPE_POKETHLON || wk->shop_type == SHOP_TYPE_POKETHLON_CARD ){
		return _psitem_price_get( item, wk->psitem, wk->max);
	}
	return 100;
}
#endif

//------------------------------------------------------------------
/**
 * @brief   指定のどうぐの個数をBMPに描画
 *
 * @param   wk
 * @param   win
 * @param   price
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void	ItemPricePrint( WORDSET *wset, MSGDATA_MANAGER *msg_man, GF_BGL_BMPWIN *win, int price, int type )
{
	STRBUF *str;
	int msgid = mes_shop_02_07;

	if(type==SHOP_TYPE_POKETHLON || type==SHOP_TYPE_POKETHLON_CARD){
		msgid = mes_shop_02_07_01;
	}

	WORDSET_RegisterNumber( wset, 0, price, 4, NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_DEFAULT );
	str = MSGDAT_UTIL_AllocExpandString( wset, msg_man, msgid, HEAPID_SUBLCD_MENU );
	GF_STR_PrintColor(	win, FONT_SYSTEM, str,
						36, 16, MSG_NO_PUT, BAGCOL_N_BLACK, NULL );
	STRBUF_Delete(str);
}


#define	GOLD_BUFLEN		( 16 )
#define	GOLD_EX			( BMP_GOLD_SX * 8 )

#define	PAGE_BUFLEN		( 5+1 )		// 「??/??」＋終端の長さ
#define ITEMNUM_BUFLEN	( 4+1 )		// どうぐ所持数＋終端の長さ
#define NUMBER_BUFLEN	( 1+1 )		// 個数表示
#define PAYPRICE_BUFLEN	( 7+1 )		// 「(6桁)円」＋終端の長さ


//--------------------------------------------------------------------------------------------
/**
 * 所持金表示
 *
 * @param	wk		ショップワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void _shop_gold_print( SHOP_SUBLCD_WORK * wk )
{
	STRBUF * expb;
	STRBUF * strb;
	u32	gold;
	u32	siz;

	// 「おこづかい」描画
	GF_BGL_BmpWinDataFill( &wk->win[WIN_GOLD], 0 );
	if( wk->shop_work->shop_type == SHOP_TYPE_POKETHLON || wk->shop_work->shop_type == SHOP_TYPE_POKETHLON_CARD){
		strb = MSGMAN_AllocString( wk->msgman, mes_shop_05_04 );
	}else{
		strb = MSGMAN_AllocString( wk->msgman, mes_shop_05_01 );
	}
	GF_STR_PrintColor( &wk->win[WIN_GOLD], FONT_SYSTEM, strb, 0, 0, MSG_NO_PUT, BAGCOL_N_WHITE, NULL );
	STRBUF_Delete( strb );


	// 所持金描画
	expb = STRBUF_Create( GOLD_BUFLEN, HEAPID_WORLD );
	if( wk->shop_work->shop_type == SHOP_TYPE_POKETHLON || wk->shop_work->shop_type == SHOP_TYPE_POKETHLON_CARD){
		strb = MSGMAN_AllocString( wk->msgman, mes_shop_05_03 );
		gold = PkthlnSv_GetThlnPoint ( wk->pkthln_sv );
	}else{
		strb = MSGMAN_AllocString( wk->msgman, mes_shop_05_02 );
		gold = MyStatus_GetGold( wk->my );
	}
	WORDSET_RegisterNumber(
		wk->wordset, 0, gold, 6, NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT );
	WORDSET_ExpandStr( wk->wordset, expb, strb );
	siz = FontProc_GetPrintStrWidth( FONT_SYSTEM, expb, 0 );
	GF_STR_PrintColor(
		&wk->win[WIN_GOLD], FONT_SYSTEM, expb, GOLD_EX-siz, 16, MSG_NO_PUT, BAGCOL_N_WHITE, NULL );
	STRBUF_Delete( strb );
	STRBUF_Delete( expb );
	GF_BGL_BmpWinOnVReq( &wk->win[WIN_GOLD] );
}


//------------------------------------------------------------------
/**
 * @brief   ページ数描画
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _shop_page_print( SHOP_SUBLCD_WORK *wk )
{
	STRBUF * expb;
	STRBUF * strb;
	int page, max;

	// ページ数「？？/？？」描画
	GF_BGL_BmpWinDataFill( &wk->win[WIN_PAGE], 0 );

	expb = STRBUF_Create( PAGE_BUFLEN, HEAPID_WORLD );
	strb = MSGMAN_AllocString( wk->msgman, mes_shop_11 );

	page = (wk->shop_work->scr+6) / SHOP_PAGE_ITEM_NUM;
	max  = wk->shop_work->max / SHOP_PAGE_ITEM_NUM;
	if(wk->shop_work->max % SHOP_PAGE_ITEM_NUM){
		max ++;
	}

	WORDSET_RegisterNumber(	wk->wordset, 0, page, 2, NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT );
	WORDSET_RegisterNumber(	wk->wordset, 1, max,  2, NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT );
	WORDSET_ExpandStr( wk->wordset, expb, strb );
	GF_STR_PrintColor(
		&wk->win[WIN_PAGE], FONT_SYSTEM, expb, 0, 0, MSG_NO_PUT, BAGCOL_N_WHITE, NULL );
	STRBUF_Delete( strb );
	STRBUF_Delete( expb );

	GF_BGL_BmpWinOnVReq( &wk->win[WIN_PAGE] );

}


#define ORDER_NONE 		( 0 )
#define ORDER_PASTE 	( 1 )
#define ORDER_CUT 	 	( 2 )

typedef struct{
	u8 command;					// ORDER_NONE, ORDER_PASTE(領域内でコピペ), ORDER_CUT（削除）
	u8 cx, cy, px, py, w, h;	// コピー開始XY,コピー先XY,横幅,縦幅
	u8 dummy;
}CUT_PASTE_DATA;

#define BG_ORDER_MAX	( 4 )

/// スクリーンの切り貼りを行うためのコマンドテーブル
static CUT_PASTE_DATA bg_cutpaste_tbl[][BG_ORDER_MAX]={
	{
		{ ORDER_NONE,  0,  0, 0, 0,  0, 0, },{ ORDER_CUT,   0,  0, 0, 4,32,16,  },
		{ ORDER_NONE,  0,  0, 0, 0,  0, 0, },{ ORDER_NONE,  0,  0,16, 4,16,16,  },
	},

	{	// 1
		{ ORDER_PASTE, 0, 19, 0, 9, 16, 1, },{ ORDER_CUT,   0,  0, 0,10,16,10,  },
		{ ORDER_NONE,  0,  0, 0, 0,  0, 0, },{ ORDER_CUT,   0,  0,16, 4,16,16,  },
	},
	{	// 2
		{ ORDER_PASTE, 0, 19, 0, 9, 16, 1, },{ ORDER_NONE,  0,  0, 0, 0, 0, 0,  },
		{ ORDER_PASTE,16, 19,16, 9, 16, 1, },{ ORDER_CUT,   0,  0, 0,10,32,10,  },
	},
	{	// 3
		{ ORDER_PASTE, 0, 19, 0,14, 16, 1, },{ ORDER_CUT,   0,  0, 0,15,16, 5,  },
		{ ORDER_PASTE,16, 19,16, 9, 16, 1, },{ ORDER_CUT,   0,  0,16,10,16,10,  },
	},
	{	// 4
		{ ORDER_PASTE, 0, 19, 0,14, 16, 1, },{ ORDER_NONE,  0,  0, 0, 0, 0, 0,  },
		{ ORDER_PASTE,16, 19,16,14, 16, 1, },{ ORDER_CUT,   0,  0, 0,15,32, 5,  },
	},
	{	// 5
		{ ORDER_NONE,  0,  0, 0, 0,  0, 0, },{ ORDER_NONE,  0,  0, 0, 0, 0, 0,  },
		{ ORDER_PASTE,16, 19,16,14, 16, 1, },{ ORDER_CUT,   0,  0,16,15,16, 5,  },
	},
};


//------------------------------------------------------------------
/**
 * @brief   ボタン面を切り貼りする
 *
 * @param   bgl
 * @param   frm
 * @param   num		どうぐの数
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void make_button_screen( GF_BGL_INI *bgl, int frm, int num )
{
	u16 *buf = GF_BGL_ScreenAdrsGet( bgl, frm );
	int  i;

	if(num==6){
		return;
	}

	for(i=0;i<BG_ORDER_MAX;i++){
		CUT_PASTE_DATA *cpd = &bg_cutpaste_tbl[num][i];
		if(cpd->command==ORDER_PASTE){
			// 指定部分をコピペ
			GF_BGL_ScrWrite( bgl, frm, &buf[cpd->cy*32+cpd->cx], cpd->px, cpd->py, cpd->w, cpd->h);
		}else if(cpd->command==ORDER_CUT){
			// 指定矩形をカット
			GF_BGL_ScrFill(	bgl, frm, 0, cpd->px, cpd->py, cpd->w, cpd->h, 0 );
		}
	}
}


//------------------------------------------------------------------
/**
 * @brief   どうぐの個数にあわせたボタン面を作成・転送
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _button_screen_set( SHOP_SUBLCD_WORK *wk, int num )
{
	NNSG2dScreenData *scrnData;
	void 			 *scrnBuf;

	// ボタン面セット(一旦６つボタンのスクリーンを読み込む）
	scrnBuf = ArcUtil_ScrnDataGet(ARC_SHOP_GRA, NARC_shop_gra_itemBG02_d_NSCR,0, &scrnData, HEAPID_SUBLCD_MENU);
	GF_BGL_ScreenBufSet( wk->bgl, GF_BGL_FRAME1_S, scrnData->rawData, scrnData->szByte );
//	DC_FlushRange( GF_BGL_ScreenAdrsGet( wk->bgl, GF_BGL_FRAME1_S ), scrnData->szByte );

	// 表示するどうぐの個数にあわせてカット
	make_button_screen( wk->bgl, GF_BGL_FRAME1_S, num );

	GF_BGL_LoadScreenV_Req( wk->bgl, GF_BGL_FRAME1_S );
	sys_FreeMemoryEz(scrnBuf);

}


#define POKETHLON_DATA_1LEVEL_MAX	( 6 )
//------------------------------------------------------------------
/**
 * @brief   値段表示をするか
 *
 * @param   sw
 * @param   pos
 *
 * @retval  int		1:する	0:しない
 */
//------------------------------------------------------------------
static int _price_print_check( SHOP_WORK *sw, int pos, int item )
{
	if(sw->shop_type==SHOP_TYPE_POKETHLON ){
		if(PkthlnSv_GetShopFlag ( sw->pokethlon_save, pos )){
			return 0;
		}else{
			return 1;
		}
	}else if(sw->shop_type==SHOP_TYPE_POKETHLON_CARD){
		int index = (item-ITEM_DEETAKAADO01)/POKETHLON_DATA_1LEVEL_MAX;
		index *= POKETHLON_DATA_1LEVEL_MAX;
		if(PkthlnSv_GetRankingAppear ( sw->pokethlon_save, pos+index )){
			return 0;
		}else{
			return 1;
		}
	}

	// ポケスロンショップと関係なかったら常に値段は表示
	return 1;
}


//------------------------------------------------------------------
/**
 * @brief   センターに表示するどうぐ文字列描画
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _select_item_print( SHOP_SUBLCD_WORK *wk )
{
	int pos     = wk->shop_work->cursor_pos+wk->shop_work->scr;
	STRBUF *str = MSGMAN_AllocString( wk->item_man, wk->shop_work->item[pos] );


	itemname_print( wk, &wk->sub_win[ADD_WIN_SELECT_ITEM], str, pos );
	STRBUF_Delete(str);

	if(_price_print_check(wk->shop_work, pos, wk->shop_work->item[pos])){
		// 値段描画
		ItemPricePrint( wk->wordset, wk->msgman, &wk->sub_win[ADD_WIN_SELECT_ITEM],
						ShopBuyItemPriceGet( wk->shop_work, wk->shop_work->item[pos] ),
						wk->shop_work->shop_type );
	}

	GF_BGL_BmpWinOnVReq( &wk->sub_win[ADD_WIN_SELECT_ITEM] );
}

//------------------------------------------------------------------
/**
 * @brief   「もってるかず」とどうぐ所持数を描画
 *
 * @param   wk
 * @param   item
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _inhand_num_print( SHOP_SUBLCD_WORK *wk, int item )
{
	STRBUF *str, *expb, *strb;
	int num,width;

	// 「もってるかず」描画
	str = MSGMAN_AllocString( wk->msgman, mes_shop_06_02 );
	GF_STR_PrintColor(
		&wk->sub_win[ADD_WIN_INHAND], FONT_SYSTEM, str, 0, 4, MSG_NO_PUT, BAGCOL_N_BLACK, NULL );
	STRBUF_Delete(str);


	// 自分のもっているどうぐの数を取得して描画
	expb = STRBUF_Create( ITEMNUM_BUFLEN, HEAPID_WORLD );
	strb = MSGMAN_AllocString( wk->msgman, mes_shop_06_03 );

	num = Shop_GetMyItemNum( wk->shop_work, wk->shop_work->sel_item );

	WORDSET_RegisterNumber(	wk->wordset, 0, num, 3, NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT );
	WORDSET_ExpandStr( wk->wordset, expb, strb );

	width = FontProc_GetPrintStrWidth( FONT_SYSTEM, expb, 0 );

	GF_STR_PrintColor(
		&wk->sub_win[ADD_WIN_INHAND], FONT_SYSTEM, expb, BMP_INHAND_W*8-width,
		20, MSG_NO_PUT, BAGCOL_N_BLACK, NULL );
	STRBUF_Delete(expb);
	STRBUF_Delete(strb);

	GF_BGL_BmpWinOnVReq( &wk->sub_win[ADD_WIN_INHAND] );
}


//------------------------------------------------------------------
/**
 * @brief   購入する数と購入金額の表示
 *
 * @param   wk
 * @param   sel_num	購入する数
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _pay_num_print( SHOP_SUBLCD_WORK *wk, int sel_num )
{
	STRBUF *expb, *expb10, *expb1, *pricestr, *strb10, *strb1;
	int num10, num1, width;

	// 現在購入しようとしているどうぐの個数を描画
	expb10 = STRBUF_Create( NUMBER_BUFLEN, HEAPID_WORLD );
	expb1  = STRBUF_Create( NUMBER_BUFLEN, HEAPID_WORLD );
	strb10 = MSGMAN_AllocString( wk->msgman, mes_shop_12_01 );
	strb1  = MSGMAN_AllocString( wk->msgman, mes_shop_12_02 );

	WORDSET_RegisterNumber(	wk->wordset, 0, sel_num/10, 1,
							NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT );
	WORDSET_RegisterNumber(	wk->wordset, 1, sel_num%10, 1,
							NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT );
	WORDSET_ExpandStr( wk->wordset, expb10, strb10 );
	WORDSET_ExpandStr( wk->wordset, expb1,  strb1 );

	GF_BGL_BmpWinDataFill( &wk->sub_win[ADD_WIN_NUM_10], 0 );
	GF_BGL_BmpWinDataFill( &wk->sub_win[ADD_WIN_NUM_1], 0 );

	GF_STR_PrintColor(
		&wk->sub_win[ADD_WIN_NUM_10], FONT_SYSTEM, expb10, 0, 4, MSG_NO_PUT, BAGCOL_N_BLACK, NULL );
	GF_STR_PrintColor(
		&wk->sub_win[ADD_WIN_NUM_1], FONT_SYSTEM,  expb1,  0, 4, MSG_NO_PUT, BAGCOL_N_BLACK, NULL );
	STRBUF_Delete(expb10);
	STRBUF_Delete(expb1);
	STRBUF_Delete(strb10);
	STRBUF_Delete(strb1);


	GF_BGL_BmpWinOnVReq( &wk->sub_win[ADD_WIN_NUM_10] );
	GF_BGL_BmpWinOnVReq( &wk->sub_win[ADD_WIN_NUM_1] );




	// 購入する際の金額を表示
	GF_BGL_BmpWinDataFill( &wk->sub_win[ADD_WIN_PAY], 0 );

	expb   = STRBUF_Create( PAYPRICE_BUFLEN, HEAPID_WORLD );
	pricestr = MSGMAN_AllocString( wk->msgman, mes_shop_07_02 );

	WORDSET_RegisterNumber(	wk->wordset, 0, wk->shop_work->sel_price*sel_num, 6,
							NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT );
	WORDSET_ExpandStr( wk->wordset, expb, pricestr );

	width = FontProc_GetPrintStrWidth( FONT_SYSTEM, expb, 0 );

	GF_STR_PrintColor(
		&wk->sub_win[ADD_WIN_PAY], FONT_SYSTEM, expb, BMP_PAY_W*8-width,
		4, MSG_NO_PUT, BAGCOL_N_BLACK, NULL );
	STRBUF_Delete(expb);
	STRBUF_Delete(pricestr);

	GF_BGL_BmpWinOnVReq( &wk->sub_win[ADD_WIN_PAY] );


}

//------------------------------------------------------------------
/**
 * @brief   「かう」文字列描画
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _buy_str_print( SHOP_SUBLCD_WORK *wk )
{
	STRBUF *str = MSGMAN_AllocString( wk->msgman, mes_shop_10 );

	GF_STR_PrintColor(
		&wk->sub_win[ADD_WIN_BUY], FONT_SYSTEM, str, 4, 0, MSG_NO_PUT, BAGCOL_N_WHITE, NULL );
	GF_BGL_BmpWinOnVReq( &wk->sub_win[ADD_WIN_BUY] );

	STRBUF_Delete(str);
}
static void ShopBuyItemNameWordSet( int type, WORDSET *wset, u16 item, u16 bufID );

//--------------------------------------------------------------------------------------------
/**
 * アイテム名を単語登録
 *
 * @param	wk		ショップワーク
 * @param	item	アイテムID
 * @param	bufID	単語ID
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void ShopBuyItemNameWordSet( int type, WORDSET *wset, u16 item, u16 bufID )
{
	switch(type){
	case SHOP_TYPE_ITEM: case SHOP_TYPE_POKETHLON: case SHOP_TYPE_POKETHLON_CARD:
		WORDSET_RegisterItemName( wset, bufID, item );
		break;
	case SHOP_TYPE_GOODS:
		WORDSET_RegisterUGGoodsName( wset, bufID, item );
		break;
	default:
//		WORDSET_RegisterSealName( wset, bufID, Seal_NameGet((u8)item) );
		break;
	}
}


//------------------------------------------------------------------
/**
 * @brief   「●●をいくつ買いますか？」描画開始
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _sale_talk_start( SHOP_SUBLCD_WORK *wk )
{
	STRBUF *str;

	GF_BGL_BmpWinDataFill(&wk->sub_win[ADD_WIN_MINITALK], 15 );

	ShopBuyItemNameWordSet( wk->shop_work->shop_type, wk->wordset, wk->shop_work->sel_item, 0 );

	str = MSGMAN_AllocString( wk->msgman, mes_shop_02_02 );
	WORDSET_ExpandStr( wk->wordset, wk->talkstr, str );
	STRBUF_Delete( str );

	TalkWinGraphicSet(
		wk->bgl, GF_BGL_FRAME0_S, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL,
		CONFIG_GetWindowType(wk->config), HEAPID_FIELD );

	BmpTalkWinWrite( &wk->sub_win[ADD_WIN_MINITALK], WINDOW_TRANS_OFF, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL);
	wk->shop_work->midx = GF_STR_PrintSimple( &wk->sub_win[ADD_WIN_MINITALK], FONT_TALK,
										wk->talkstr, 0, 0, CONFIG_GetMsgPrintSpeed(wk->config), NULL );
//	wk->midx = FieldTalkMsgStart( &wk->win[WIN_IDX_TALK], wk->msgb, wk->cfg, 1 );

}


//------------------------------------------------------------------
/**
 * @brief
 *
 * @param  none
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _price_talk_start( SHOP_SUBLCD_WORK *wk )
{
	STRBUF *str;

	GF_BGL_BmpWinDataFill(&wk->sub_win[ADD_WIN_MINITALK], 15 );

	if(wk->shop_work->shop_type==SHOP_TYPE_POKETHLON || wk->shop_work->shop_type==SHOP_TYPE_POKETHLON_CARD){
		ShopBuyItemNameWordSet( wk->shop_work->shop_type, wk->wordset, wk->shop_work->sel_item, 0 );
		str = MSGMAN_AllocString( wk->msgman, mes_shop_13_01 );
	}else{
		WORDSET_RegisterNumber(	wk->wordset, 0, wk->shop_work->sel_num, 2,
								NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_DEFAULT );
		WORDSET_RegisterNumber(	wk->wordset, 1, wk->shop_work->sel_price*wk->shop_work->sel_num, 6,
								NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_DEFAULT );
		str = MSGMAN_AllocString( wk->msgman, mes_shop_02_03_01 );
	}

	WORDSET_ExpandStr( wk->wordset, wk->talkstr, str );
	STRBUF_Delete( str );

	TalkWinGraphicSet(
		wk->bgl, GF_BGL_FRAME0_S, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL,
		CONFIG_GetWindowType(wk->config), HEAPID_FIELD );
	BmpTalkWinWrite( &wk->sub_win[ADD_WIN_MINITALK], WINDOW_TRANS_OFF, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL);
	wk->msg_index = GF_STR_PrintSimple( &wk->sub_win[ADD_WIN_MINITALK], FONT_TALK,
										wk->talkstr, 0, 0, CONFIG_GetMsgPrintSpeed(wk->config), NULL );
//	wk->midx = FieldTalkMsgStart( &wk->win[WIN_IDX_TALK], wk->msgb, wk->cfg, 1 );


}


//------------------------------------------------------------------
/**
 * @brief   タッチYESNOスタート
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _touch_sw_yesno_start( SHOP_SUBLCD_WORK *wk )
{
	TOUCH_SW_PARAM tp_param;
	MI_CpuClear8(&tp_param,sizeof(TOUCH_SW_PARAM));

	// 初期化パラメータ設定
	tp_param.p_bgl 		= wk->bgl;
	tp_param.bg_frame	= GF_BGL_FRAME1_S;
	tp_param.char_offs	= 32*3;
	tp_param.pltt_offs	= 8;
	tp_param.x			= 26;
	tp_param.y			= 6;
	tp_param.kt_st      = KeyTouchStatus_CheckTouchOrKey(&wk->fsys->KeyTouchStatus); //現在のキータッチステータス
	tp_param.key_pos    = 0;			//yesがカレント
	tp_param.type       = TOUCH_SW_TYPE_S;

	GF_BGL_ScrClear( wk->bgl, GF_BGL_FRAME1_S );

	//はい・いいえタッチパネルシステム読み込み
	wk->tpSWsys = TOUCH_SW_AllocWork( HEAPID_SUBLCD_MENU );
	TOUCH_SW_Init( wk->tpSWsys, &tp_param );

}


//------------------------------------------------------------------
/**
 * @brief   レポートを書きますか？のはい・いいえ待ち
 *
 * @param   rsw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int _touch_sw_yesno_wait( SHOP_SUBLCD_WORK *wk )
{
	int result;
	if(wk->tpSWsys==NULL){
		return FALSE;
	}
	result = TOUCH_SW_MainMC( wk->tpSWsys );

	switch(result){
	case TOUCH_SW_RET_YES:
		//はい・いいえタッチパネルシステム破棄
		TOUCH_SW_FreeWork( wk->tpSWsys );
		wk->tpSWsys = NULL;
		wk->shop_work->touch_arret = 0;
		return TRUE;
		break;
	case TOUCH_SW_RET_NO:
		//はい・いいえタッチパネルシステム破棄
		TOUCH_SW_FreeWork( wk->tpSWsys );
		wk->tpSWsys = NULL;
		wk->shop_work->touch_arret = 1;
		return TRUE;
		break;
	}


	return FALSE;

}

//------------------------------------------------------------------
/**
 * @brief   「はい　まいどありがとうございます」
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _item_buy_message_start( SHOP_SUBLCD_WORK *wk )
{

	STRBUF * str;

	ShopBuyItemNameWordSet( wk->shop_work->shop_type, wk->wordset, wk->shop_work->sel_item, 0 );
	switch(wk->shop_work->shop_type){
	case SHOP_TYPE_ITEM:
		str = MSGMAN_AllocString( wk->msgman, mes_shop_02_04 );
		WORDSET_RegisterItemPocketWithIcon(
			wk->wordset, 1, ItemParamGet( wk->shop_work->sel_item, ITEM_PRM_POCKET, HEAPID_WORLD ) );
		break;
	case SHOP_TYPE_POKETHLON:
		if( wk->shop_work->sel_item >= ITEM_AKABONGURI && wk->shop_work->sel_item <= ITEM_KUROBONGURI ){
			str = MSGMAN_AllocString( wk->msgman, mes_shop_15 );
		}else{
			str = MSGMAN_AllocString( wk->msgman, mes_shop_02_04 );
			WORDSET_RegisterItemPocketWithIcon(
				wk->wordset, 1, ItemParamGet( wk->shop_work->sel_item, ITEM_PRM_POCKET, HEAPID_WORLD ) );
		}
		break;
	case SHOP_TYPE_POKETHLON_CARD:
		str = MSGMAN_AllocString( wk->msgman, mes_shop_15 );
		break;
	case SHOP_TYPE_GOODS:
		str = MSGMAN_AllocString( wk->msgman, mes_shop_03_01 );
		break;
	default:
		str = MSGMAN_AllocString( wk->msgman, mes_shop_03_03 );
		break;
	}
	WORDSET_ExpandStr( wk->wordset, wk->talkstr, str );
	STRBUF_Delete( str );

	GF_BGL_BmpWinDataFill( &wk->win[WIN_TALK], 15 );
	BmpTalkWinWrite( &wk->win[WIN_TALK], WINDOW_TRANS_OFF, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL);
	wk->shop_work->midx = GF_STR_PrintSimple( &wk->win[WIN_TALK], FONT_TALK,
										wk->talkstr, 0, 0, CONFIG_GetMsgPrintSpeed(wk->config), TalkMsgCallBack );

}

//--------------------------------------------------------------------------------------------
/**
 * メッセージコールバック
 *
 * @param	mph		メッセージ描画データ
 * @param	value	コールバックナンバ
 *
 * @retval	"TRUE = 待ち"
 * @retval	"FALSE = 次の処理へ"
 */
//--------------------------------------------------------------------------------------------
static BOOL TalkMsgCallBack( MSG_PRINT_HEADER * mph, u16 value )
{
	switch( value ){
	case 1:		// 買った後のＳＥ再生
		Snd_SePlay( SEQ_SE_DP_REGI );
		break;
	}

	return FALSE;
}


//------------------------------------------------------------------
/**
 * @brief   買えない理由文字列を取得
 *
 * @param   wk
 * @param   msg_man
 *
 * @retval  STRBUF *
 */
//------------------------------------------------------------------
static STRBUF *_get_cant_buy_string( SHOP_WORK *wk, MSGDATA_MANAGER *msgman )
{
	int result,gold;

	if(wk->shop_type==SHOP_TYPE_POKETHLON || wk->shop_type==SHOP_TYPE_POKETHLON_CARD){
		gold = PkthlnSv_GetThlnPoint ( wk->pokethlon_save );
	}else{
		gold = MyStatus_GetGold( wk->my );
	}
	result = ShopCheckBuyItem( wk, gold );

	MORI_PRINT("buy check result=%d\n",result);

	if(result==2){
		return MSGMAN_AllocString( msgman, mes_shop_03_05 );	// きょうはもう買えませんよ！
	}else if(result==3){
		return MSGMAN_AllocString( msgman, mes_shop_03_06 );	// もう持ってますよ！
	}

	if(wk->shop_type==SHOP_TYPE_POKETHLON || wk->shop_type==SHOP_TYPE_POKETHLON_CARD){
		return MSGMAN_AllocString( msgman, mes_shop_14 );		// ポイントがたりませんよ！
	}

	return MSGMAN_AllocString( msgman, mes_shop_02_01 );		// お金がたりませんよ！
}

//------------------------------------------------------------------
/**
 * @brief   個数判定による購入失敗 文字列を取得
 *
 * @param   wk
 * @param   msg_man
 *
 * @retval  STRBUF *
 */
//------------------------------------------------------------------
static STRBUF *_get_cmp_false_string( SHOP_WORK *wk, MSGDATA_MANAGER *msgman )
{
	int msg_idx;

	if( wk->shop_type == SHOP_TYPE_ITEM ){
		msg_idx = mes_shop_02_05;
	}else if( wk->shop_type == SHOP_TYPE_GOODS ){
		msg_idx = mes_shop_03_02;
	}else if( wk->shop_type == SHOP_TYPE_SEAL ){
		msg_idx = mes_shop_03_04;
	}else if( wk->shop_type == SHOP_TYPE_POKETHLON ){
		msg_idx = mes_shop_03_05;
	}else if( wk->shop_type == SHOP_TYPE_POKETHLON_CARD ){
		msg_idx = mes_shop_03_06;
	}

	return MSGMAN_AllocString( msgman, msg_idx );
}

//------------------------------------------------------------------
/**
 * @brief   おかねがたりませんよ！
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _cant_buy_talk_start( SHOP_SUBLCD_WORK *wk )
{

	STRBUF *str;

	// 購入できなかった時の文字列を取得
	str = _get_cant_buy_string( wk->shop_work, wk->msgman );

	WORDSET_ExpandStr( wk->wordset, wk->talkstr, str );
	STRBUF_Delete( str );
	GF_BGL_BmpWinDataFill( &wk->win[WIN_TALK], 15 );

	TalkWinGraphicSet(
		wk->bgl, GF_BGL_FRAME0_S, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL,
		CONFIG_GetWindowType(wk->config), HEAPID_FIELD );

	BmpTalkWinWrite( &wk->win[WIN_TALK], WINDOW_TRANS_OFF, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL);
	wk->shop_work->midx = GF_STR_PrintSimple( &wk->win[WIN_TALK], FONT_TALK,
										wk->talkstr, 0, 0, CONFIG_GetMsgPrintSpeed(wk->config), NULL );

}

//------------------------------------------------------------------
/**
 * @brief   個数判定による失敗メッセージ
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _cmp_false_talk_start( SHOP_SUBLCD_WORK *wk )
{
	STRBUF *str;

	// 購入できなかった時の文字列を取得
	str = _get_cmp_false_string( wk->shop_work, wk->msgman );

	WORDSET_ExpandStr( wk->wordset, wk->talkstr, str );
	STRBUF_Delete( str );
	GF_BGL_BmpWinDataFill( &wk->win[WIN_TALK], 15 );

	TalkWinGraphicSet(
		wk->bgl, GF_BGL_FRAME0_S, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL,
		CONFIG_GetWindowType(wk->config), HEAPID_FIELD );

	BmpTalkWinWrite( &wk->win[WIN_TALK], WINDOW_TRANS_OFF, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL);
	wk->shop_work->midx = GF_STR_PrintSimple( &wk->win[WIN_TALK], FONT_TALK,
										wk->talkstr, 0, 0, CONFIG_GetMsgPrintSpeed(wk->config), NULL );
}

static void _cmp_false_talk_start2( SHOP_SUBLCD_WORK *wk )
{
	STRBUF *str;

	// 購入できなかった時の文字列を取得
	str = MSGMAN_AllocString( wk->msgman, mes_shop_02_05 );

	WORDSET_ExpandStr( wk->wordset, wk->talkstr, str );
	STRBUF_Delete( str );
	GF_BGL_BmpWinDataFill( &wk->win[WIN_TALK], 15 );

	TalkWinGraphicSet(
		wk->bgl, GF_BGL_FRAME0_S, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL,
		CONFIG_GetWindowType(wk->config), HEAPID_FIELD );

	BmpTalkWinWrite( &wk->win[WIN_TALK], WINDOW_TRANS_OFF, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL);
	wk->shop_work->midx = GF_STR_PrintSimple( &wk->win[WIN_TALK], FONT_TALK,
										wk->talkstr, 0, 0, CONFIG_GetMsgPrintSpeed(wk->config), NULL );

}

//-----------------------------------------------------------------------------
/**
 *	@brief	プレミアムボールどうぞ
 *
 *	@param	SHOP_SUBLCD_WORK* wk
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void _puremium_message_talk_start( SHOP_SUBLCD_WORK* wk )
{
	STRBUF *str;

	str = MSGMAN_AllocString( wk->msgman, mes_shop_02_08 );
	WORDSET_ExpandStr( wk->wordset, wk->talkstr, str );
	STRBUF_Delete( str );

	GF_BGL_BmpWinDataFill( &wk->win[WIN_TALK], 15 );

	TalkWinGraphicSet(
		wk->bgl, GF_BGL_FRAME0_S, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL,
		CONFIG_GetWindowType(wk->config), HEAPID_FIELD );

	BmpTalkWinWrite( &wk->win[WIN_TALK], WINDOW_TRANS_OFF, TALK_WIN_FRAME_CGX, TALK_WINDOW_PAL);
	wk->shop_work->midx = GF_STR_PrintSimple( &wk->win[WIN_TALK], FONT_TALK,
										wk->talkstr, 0, 0, CONFIG_GetMsgPrintSpeed(wk->config), NULL );

}


//------------------------------------------------------------------
/**
 * @brief   選択後画面用のBMPWINを確保する
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void SelectItemBmp_Init( SHOP_SUBLCD_WORK *wk )
{
	// 選択したアイテムと値段表示領域
	GF_BGL_BmpWinAdd(	wk->bgl, &wk->sub_win[ADD_WIN_SELECT_ITEM],
		BMP_LIST_FRM,
		BMP_SELECTITEM_PX, BMP_SELECTITEM_PY, BMP_ITEM_W, BMP_ITEM_H, BMP_LIST_PAL,  BMP_SELECTITEM_CGX);
	// １０の桁の数
	GF_BGL_BmpWinAdd(	wk->bgl, &wk->sub_win[ADD_WIN_NUM_10],
		BMP_NUM_FRM,
		BMP_NUM_10_PX, BMP_NUM_10_PY, BMP_NUM_W, BMP_NUM_H, BMP_LIST_PAL,  BMP_NUM_10_CGX);
	// １の桁の数
	GF_BGL_BmpWinAdd(	wk->bgl, &wk->sub_win[ADD_WIN_NUM_1],
		BMP_NUM_FRM,
		BMP_NUM_1_PX, BMP_NUM_1_PY, BMP_NUM_W, BMP_NUM_H, BMP_LIST_PAL,  BMP_NUM_1_CGX);
	// 「かう」
	GF_BGL_BmpWinAdd(	wk->bgl, &wk->sub_win[ADD_WIN_BUY],
		BMP_BUY_FRM,
		BMP_BUY_PX, BMP_BUY_PY, BMP_BUY_W, BMP_BUY_H, BMP_LIST_PAL,  BMP_BUY_CGX);
	// 「もっているかず」とアイテム所持数
	GF_BGL_BmpWinAdd(	wk->bgl, &wk->sub_win[ADD_WIN_INHAND],
		BMP_INHAND_FRM,
		BMP_INHAND_PX, BMP_INHAND_PY, BMP_INHAND_W, BMP_INHAND_H, BMP_INHAND_PAL,  BMP_INHAND_CGX);
	// 購入金額
	GF_BGL_BmpWinAdd(	wk->bgl, &wk->sub_win[ADD_WIN_PAY],
		BMP_PAY_FRM,
		BMP_PAY_PX, BMP_PAY_PY, BMP_PAY_W, BMP_PAY_H, BMP_LIST_PAL,  BMP_PAY_CGX);
	// 小さい会話ウインドウ
	GF_BGL_BmpWinAdd(	wk->bgl, &wk->sub_win[ADD_WIN_MINITALK],
		BMP_MINITALK_FRM,
		BMP_MINITALK_PX, BMP_MINITALK_PY, BMP_MINITALK_W, BMP_MINITALK_H, BMP_LIST_PAL,  BMP_MINITALK_CGX);


	GF_BGL_BmpWinDataFill( &wk->sub_win[ADD_WIN_SELECT_ITEM], 0 );
	GF_BGL_BmpWinDataFill( &wk->sub_win[ADD_WIN_NUM_10], 0 );
	GF_BGL_BmpWinDataFill( &wk->sub_win[ADD_WIN_NUM_1], 0 );
	GF_BGL_BmpWinDataFill( &wk->sub_win[ADD_WIN_BUY], 0 );
	GF_BGL_BmpWinDataFill( &wk->sub_win[ADD_WIN_INHAND], 0 );
	GF_BGL_BmpWinDataFill( &wk->sub_win[ADD_WIN_PAY], 0 );
	GF_BGL_BmpWinDataFill( &wk->sub_win[ADD_WIN_MINITALK], 15 );

}

//------------------------------------------------------------------
/**
 * @brief   選択後画面用のBMPWINを解放する
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void SelectItemBmp_Del( SHOP_SUBLCD_WORK *wk )
{

	GF_BGL_BmpWinOffVReq( &wk->sub_win[ADD_WIN_SELECT_ITEM] );
	GF_BGL_BmpWinOffVReq( &wk->sub_win[ADD_WIN_NUM_10] );
	GF_BGL_BmpWinOffVReq( &wk->sub_win[ADD_WIN_NUM_1] );
	GF_BGL_BmpWinOffVReq( &wk->sub_win[ADD_WIN_BUY] );
	GF_BGL_BmpWinOffVReq( &wk->sub_win[ADD_WIN_INHAND] );
	GF_BGL_BmpWinOffVReq( &wk->sub_win[ADD_WIN_PAY] );
	GF_BGL_BmpWinOffVReq( &wk->sub_win[ADD_WIN_MINITALK] );
	BmpTalkWinClear( &wk->sub_win[ADD_WIN_MINITALK], WINDOW_TRANS_OFF );
	BmpTalkWinClear( &wk->win[WIN_TALK], WINDOW_TRANS_ON );

	GF_BGL_BmpWinDel( &wk->sub_win[ADD_WIN_SELECT_ITEM] );
	GF_BGL_BmpWinDel( &wk->sub_win[ADD_WIN_NUM_10] );
	GF_BGL_BmpWinDel( &wk->sub_win[ADD_WIN_NUM_1] );
	GF_BGL_BmpWinDel( &wk->sub_win[ADD_WIN_BUY] );
	GF_BGL_BmpWinDel( &wk->sub_win[ADD_WIN_INHAND] );
	GF_BGL_BmpWinDel( &wk->sub_win[ADD_WIN_PAY] );
	GF_BGL_BmpWinDel( &wk->sub_win[ADD_WIN_MINITALK] );

}


/*
void ShopSublcdNumSelButtonBmpOff( SHOP_SUBLCD_WORK * wk )
{
	GF_BGL_BmpWinOffVReq( &wk->win[WIN_CANCEL] );
	GF_BGL_BmpWinOffVReq( &wk->sub_win[ADD_WIN_PAY] );
}
*/